// pages/activeManage/activeManage.js
var $ = require('../util/commit.js');
var page=1;
var pageMore=false;
var MoveStatus=false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tarIndex: 0,//tarbar选择的index
    scrolltop:0,
    scrollHeight:'',
    tabbar: [{
      id: 0,
      name: '全部',
    }, {
      id: 1,
      name: '发布中'
    }, {
      id: 2,
      name: '审核中'
    }, {
      id: 3,
      name: '已结束'
    },{
        id: 4,
        name: '已驳回'
    }],
    ListData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    that.loadData();
  },
  loadData:function(e){
    wx.showLoading({
      title: '加载中..',
    });
    let that=this;
    page=1;
    that.setData({
      scrolltop: 0,
    })
    $.POST({
      url:'caSRC',
      data:{
        type: that.data.tarIndex,
        page:1,
      }
    },function(e){
      pageMore=true;
      that.setData({
        ListData:e.data
      });
      wx.hideLoading();
    },function(e){
      wx.hideLoading();
    })
  },
  loadMoreData: function (e) {
    let that = this;
    console.log(pageMore)
    if (pageMore){
      pageMore=false;
      page++;
      $.POST({
        url: 'caSRC',
        data: {
          type: that.data.tarIndex,
          page: page,
        }
      }, function (e) {
        let newListData = that.data.ListData.concat(e.data);
        that.setData({
          ListData: newListData,

        });
        pageMore=true;

      }, function (e) { 
        pageMore = true;
      })
    }
    
 
  },
  // loadMoveData
  loadMoveData: function (e) {
    let that = this;
      if (e.detail.scrollTop < -30) {
        if (!that.MoveStatus) {
          that.MoveStatus = true;
          that.loadData();
        }
      }
      if (e.detail.scrollTop == 0) {
        that.MoveStatus = false;
      }
    

    if (e.detail.scrollTop > 0) {
      if (e.detail.deltaY > 5) {
        if (!that.MoveStatus) {
          that.MoveStatus = true;
          that.loadMoreData();
          console.log('loadMoreData');
        }
      }
      if (e.detail.deltaY == -1 || e.detail.deltaY == 1) {
        that.MoveStatus = false;
      }
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let pageNum = getCurrentPages();
    console.log('生命周期函数--监听页面卸载');
    console.log(pageNum);
    console.log(pageNum.length);
    if (pageNum.length > 3) {
      wx.reLaunch({
        url: '../mine/mine'
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that =this;
    that.loadData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that=this;
    that.loadMoreData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // selectTarbar选择类别
  selectTarbar: function (e) {
    let index = e.currentTarget.dataset.index;
    let that = this;
    that.setData({
      tarIndex: index,
    });
    that.loadData();
  },
  // ToActiveMDetail
  ToActiveMDetail:function(e){
    $.openWin({
      url:'../activeManage/activeMDetail',
      data:{
        id:e.currentTarget.dataset.id,
      }
    })
  }
})